package cn.tedu.controller;



import cn.tedu.pojo.Provider;
import cn.tedu.service.ProviderService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Controller //加入Spring
@RequestMapping("/sys/provider") //定义前缀
public class ProviderController {
    //注入服务接口
    @Autowired
    private ProviderService providerService;

    /**
     * @param model,在最开始极大可能只有这一个参数传递。       model SpringMVC已自动注入
     *                                       model 是动态数据,或者说是后端设定的数据，可能是数据库，可能的后端代码引入的传递给上层
     * @param proName,@RequestParam(required = false)无参，默认为null
     * @param proCode,@RequestParam映射链接和方法
     * @param pageIndex,required             = false保证可以没有这个参数，方法也能用啦！
     *                                       defaultValue = "1",本质是字符串会转换的
     * @return
     */
    @RequestMapping("/list.html") //最终映射
    public String getList(Model model // Model JSP视图数据包装
            , @RequestParam(required = false, defaultValue = "") String proName
            , @RequestParam(required = false, defaultValue = "") String proCode
            , @RequestParam(required = false, defaultValue = "1") int pageIndex) {
        int pageSize = 3;//可能null使用对象，对象默认null

        PageInfo<Provider> pageInfo = providerService.getList(proName, proCode, pageIndex, pageSize);
        //前端数据
        model.addAttribute("pi", pageInfo);
        model.addAttribute("proName", proName);
        model.addAttribute("proCode", proCode);
        model.addAttribute("pageIndex", pageIndex);
        return "providerList";
    }
    @RequestMapping(value = "/add.html",method = RequestMethod.POST)
    public String add(@ModelAttribute("provider") Provider provider) {
        //注意时间由服务器
        provider.setCreationDate(new Date());
        boolean success = providerService.add(provider);
        if (success) {
            // 重新请求到list
            //注意是HTTP路径,不是文件路径
            //l和L大小写
            return "redirect:/sys/provider/list.html";
        } else {
            return "provideradd";
        }
    }
    @RequestMapping(value = "/add.html", method = RequestMethod.GET)
    public String showAddForm(Model model) {
        model.addAttribute("provider", new Provider());
        return "provideradd";  // 返回增加供应商的页面
    }

    @PostMapping("/del.json")
    /**
     * 如果你没有使用@ResponseBody,Spring会默认将返回的Map作为视图名称进行解析，而不是直接输出JSON格式。
     */
    @ResponseBody
    //前端通常返回String,通过@RequestParam()可以自动转换其他
    public Map<String,String> delete(@RequestParam("proid") int proid){
        //Map是接口,HashMap是实现类
        Map<String,String> result = new HashMap<>();
        boolean isDeleted = providerService.delete(proid);
        if(isDeleted){
            result.put("delResult","true");
        } else {
            result.put("delResult","false");
        }
        return  result;
    }
    @GetMapping("/view/{proid}")
    public String view(@PathVariable("proid") int proid,Model model) {
        Provider byId = providerService.getById(proid);
        model.addAttribute("provider",byId);
        return "providerview";
    }
    @GetMapping("/modify/{proid}")
    public String modify(@PathVariable("proid") int proid,Model model) {
        Provider byId = providerService.getById(proid);
        model.addAttribute("provider",byId);
        return "providermodify";
    }
    @PostMapping("/modifysave.html")
    public String modifySave(@ModelAttribute Provider provider) {
        provider.setModifyDate(new Date());
        providerService.modify(provider);
        return "redirect:/sys/provider/list.html";
    }

}
